import Vue from "vue";
import Vuex from "vuex";
import {
    Message
} from 'element-ui';
import {
    transToUpperCase
} from '@/tools/helper.js'

// import students from "./modules/students";
// import teachers from "./modules/teachers";
// 全局广播
import states from "./state.js";
import Api from '@/api/index.js';
// import loginApi from "@/api/login.js";
// import Api from "@/api/common.js";
import types from "@/constants/types";
import questionType from "@/constants/questionType";
import router from '@/router/index.js';

Vue.use(Vuex);

const debug = process.env.NODE_ENV !== "production";

const defineStore = new Vuex.Store({
    modules: {
        // students,
        // teachers
    },
    state: states.rootState,
    getters: {
        uploadImgurl(state, getters, rootState) {
            return state.imgurl;
        },
        getExamPaperPositions(state, getters, rootState) {
            return state.examPaperPositions;
        },
        headerimgurl(state, getters, rootState) { //用户头像
            return state.headerimgurl;
        },
        nikename(state, getters, rootState) { //用户头像
            return state.nikename;
        },
        isShowLogin(state, getters, rootState) { // 显示登录弹窗
            return state.isShowLogin;
        },
        currentStep(state, getters, rootState) { // 注册步数
            return state.currentStep;
        },
        caseRulesList(state, getters, rootState) { // 公司制度管理
            return state.caseRulesList;
        },
        authentication(state, getters, rootState) { 
            return state.authentication;
        },
        relationQuestion(state, getters, rootState) { // 公关联题
            return state.relationQuestion;
        },
        questionList(state, getters, rootState) { // 题目列表
            return state.questionList;
        },
        questionSort(state, getters, rootState) { // 答题卡部分
            return state.questionSort;
        },
        getobjectlist(state, getters, rootState) { // 大数据列表
            return state.getobjectlist;
        },
        testPaperInfo(state, getters, rootState) { // 考试的试卷信息
            return state.testPaperInfo;
        },
        
    },
    mutations: {
        [types.NOTICEKEY](state, value) {//通知公告索引权限
            state.noticeKey = value;
            sessionStorage.setItem('noticeKey', value)
        },
		
		[types.CERTIFICATENUM](state, value) { // 未读通知数
		    state.certificateNum = value;
		    sessionStorage.setItem('certificateNum', value)
		},
        [types.NOTECOUNT](state, value) { // 未读通知数
            state.noteCount = value;
            sessionStorage.setItem('noteCount', value)
        },
        [types.TOPICLOADED](state, value) { // 是否加载完成
            state.topicLoaded = value;
        },
        [types.CURRENTSTEP](state, value) {
            // console.log(state, value)
            state.currentStep = value;
        },
        [types.TITLENAME](state, value) {
            // console.log('value', value)
            state.titleName = value;
        },
        [types.PERSONALDETAILS](state, value) {
            state.personalDetails = value;
            sessionStorage.setItem('personalDetails', JSON.stringify(value))
        },
        [types.MENUS](state, value) {
            state.menus = value;
            sessionStorage.setItem('menus', JSON.stringify(value))
        },
        [types.USERINFO](state, value) {

            state.userInfo = value;
            sessionStorage.setItem('UserInfo', JSON.stringify(value))
            sessionStorage.setItem('AccessToken', value.accessToken)
        },
        [types.ModifyNickname](state, value) {

            state.userInfo.name = value;
            sessionStorage.setItem('UserInfo', JSON.stringify(state.userInfo))
            sessionStorage.setItem('AccessToken', state.userInfo.accessToken)
        },
        Nickname(state, value) {
            state.userInfo.realName = value;
            sessionStorage.setItem('UserInfo', JSON.stringify(state.userInfo))
            sessionStorage.setItem('AccessToken', state.userInfo.accessToken)
        },
        MobilePhone(state, value) {
            state.userInfo.mobilePhone = value;
            sessionStorage.setItem('UserInfo', JSON.stringify(state.userInfo))
            sessionStorage.setItem('AccessToken', state.userInfo.accessToken)
        },

        [types.LOGINOUT](state, value) {
			console.log(state)
            // console.log("000")
            state.userInfo = {};
			state.personalDetails={}
            sessionStorage.removeItem('state');
            sessionStorage.removeItem('AccessToken');
            sessionStorage.removeItem("UserInfo");
            // alert("000")
            sessionStorage.removeItem("personalDetails");
            sessionStorage.removeItem("menus");
            sessionStorage.removeItem("userType");
            // window.location.href = "/";
            router.push('/new_index');
            // alert(JSON.parse(sessionStorage.getItem('state')))
            // alert(JSON.parse(sessionStorage.getItem('UserInfo')))
            // console.log("111")
            // console.log(JSON.parse(sessionStorage.getItem('state')))
            // console.log(JSON.parse(sessionStorage.getItem('UserInfo')))
            state.isVipUser = false;
            state.isBuy = false;
            // router.push('/Login');
            // router.push('/Home');
            // console.log("222")
            // console.log(JSON.parse(sessionStorage.getItem('state')))
            // console.log(JSON.parse(sessionStorage.getItem('UserInfo')))

        },
        //是否显示登录弹窗
        [types.ISSHOWLOGIN](state, value) {
            state.isShowLogin = value;
            sessionStorage.setItem('isShowLogin', JSON.stringify(value));
        },
        [types.CASERULESLIST](state, value) {
            state.caseRulesList = value;
            sessionStorage.setItem('caseRulesList', JSON.stringify(value));
        },
        [types.RELATIONQUESTION](state, value) {
            state.relationQuestion = value;
            sessionStorage.setItem('relationQuestion', JSON.stringify(value));
        },
        [types.QUESTIONLIST](state, value) {
            state.questionList = value;
            sessionStorage.setItem('questionList', JSON.stringify(value));
        },
        [types.QUESTIONSORT](state, value) {
            state.questionSort = value;
            sessionStorage.setItem('questionSort', JSON.stringify(value));
        },
        [types.GETOBJECTLIST](state, value) {
            state.getobjectlist = value;
            sessionStorage.setItem('getobjectlist', JSON.stringify(value));
        },
        testPaperInfo(state, value) { // 考试试卷的相关信息
            state.testPaperInfo = value;
            sessionStorage.setItem('testPaperInfo', JSON.stringify(value));
        },
    },
    actions: {
        [types.AUTHENTICATION](context, args) { 
            Api.GetPersonalDetails(args).then(res => {
                if (res) {
                    // console.log("认证", res);
                    // context.commit(types.AUTHENTICATION, res.data);
                    context.commit(types.PERSONALDETAILS, res.data);
                    //以下无用
                    // console.log(JSON.parse(sessionStorage.getItem("personalDetails")))
                    // if(sessionStorage.getItem("personalDetails")){
                    //   let personal = JSON.parse(sessionStorage.getItem("personalDetails"));
                    //   console.log(personal)
                    //   if(personal.userType==0){
                    //     sessionStorage.setItem("userType",2)
                    //   }else{
                    //     sessionStorage.setItem("userType",personal.userType)
                    //   }
                    // }
                }
            }).catch(err => {
                console.log('err', err);
            })
        },
    },
    strict: debug
});

export default defineStore;